/*
 * Rhy tab module
 * 
 * @author Tristan Maindron
 * @dependencies rhy.core.wrapper, rhy.core.selector, rhy.core.config, rhy.core.view
 * 
 */

Rhy.extra.tab = {
    data: [],
    apply: function(tabName, config){
        var tabElement = $rh('#' + tabName);
        var menuHolder = $rh('.rhy-tab-menu', tabElement)[0];
        var contentHolder = $rh('.rhy-tab-content', tabElement)[0];
        
        config = $r.cfg.manager.merge({
            onChange: function(){}
        }, config);
        
        config = $r.cfg.manager.merge({
            onFirstChange: config.onChange
        }, config);
        
        Rhy.extra.tab.data[tabName] = { 
            menuHolder : menuHolder, 
            contentHolder: contentHolder,
            onChange: config.onChange,
            onFirstChange: config.onFirstChange,
            viewed: []
        };
        
        Rhy.extra.tab.refresh(tabName);
        Rhy.extra.tab.handle(tabName, 0);
    },
    
    refresh: function(tabName){
        var menuHolder = Rhy.extra.tab.data[tabName].menuHolder;
        var menu = $rh('.rhy-tab-menu-element', menuHolder);
        for(var i = 0; i < menu.length; i++){
            menu[i].tabName = tabName;
            menu[i].position = i;
            menu[i].onclick = function(){
                Rhy.extra.tab.handle(this.tabName, this.position);
            }
        }
    },
    
    addTab: function(tabName, label, content){
        var contentHolder = Rhy.extra.tab.data[tabName].contentHolder;
        var menuHolder = Rhy.extra.tab.data[tabName].menuHolder;
        
        var menuElement = document.createElement('li');
        var contentElement = document.createElement('div');
        
        menuElement.innerHTML = label;
        contentElement.innerHTML = content;
        
        menuElement.className = 'rhy-tab-menu-element';
        contentElement.className = 'rhy-tab-content-element';
        
        contentHolder.appendChild(contentElement);
        menuHolder.appendChild(menuElement);
        
        contentElement.style.display = 'none';
        
        Rhy.extra.tab.refresh(tabName);
    },
    
    selectTab: function(tabName, position){
        Rhy.extra.tab.handle(tabName, position);
    },
    
    setTab: function(tabName, position, content){
        var contentHolder = Rhy.extra.tab.data[tabName].contentHolder;
        var contentElement = $rh('.rhy-tab-content-element', contentHolder)[position];
        contentElement.innerHTML = content;
    },
    
    handle: function(tabName, position){
        var contentHolder = Rhy.extra.tab.data[tabName].contentHolder;
        var menuHolder = Rhy.extra.tab.data[tabName].menuHolder;
        
        var content = $rh('.rhy-tab-content-element', contentHolder);
        var menu = $rh('.rhy-tab-menu-element', menuHolder);
        
        for(var i = 0; i < content.length; i++){
            content[i].style.display = 'none';
            if(i == position){
                content[i].style.display = 'block';
                menu[i].className += " rhy-tab-selected";
            }
            else{
                menu[i].className = menu[i].className.replace(" rhy-tab-selected", "");
            }
        }
        
        if(typeof(Rhy.extra.tab.data[tabName].viewed[position]) == 'undefined'){
            Rhy.extra.tab.data[tabName].viewed[position] = true;
            Rhy.extra.tab.data[tabName].onFirstChange(position);
        }
        else{
            Rhy.extra.tab.data[tabName].onChange(position);
        }
        
    }
}

